package com.mask.mr;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.zookeeper.ZooKeeper;

import java.io.IOException;

/**
 * ClassName WordReducer
 * Create by hx
 * Date 2021/9/22 2:33 下午
 * reduce: 获取的数据是经过shuffle处理之后的数据，不是map输出的<K,V>
 * 而是<K,Iterable<1,1,1,1>>
 */

public class WordReducer extends Reducer<Text,IntWritable,Text,IntWritable> {
    @Override
    protected void reduce(Text key, Iterable<IntWritable> value,
                          Context context) throws IOException, InterruptedException {
        //  <Sun,<1,1,1,...>>
        // 1. 定义一个变量来保存总数
        int sum = 0;
        // 2. 计算
        for(IntWritable v : value) {
            sum += v.get();
        }
        // 3. 输出
        context.write(key, new IntWritable(sum));
    }
}
